class TestDemo{
    public String reverseWords(String s) {
        int n = s.length();
        int l = 0;
        int r = n-1;
        int i,j,jj;
        char[] result = s.toCharArray();
        for(i = 0;i<n;i++){
            if(result[i]==' '){
                jj=i-1;
                while(l<jj){
                    char temp = result[l];
                    result[l] = result[jj];
                    result[jj] = temp;
                    l++;
                    jj--;
                }
                l=i+1;
            }
            if(i==r){
                while(l<r)
                {
                    char temp = result[l];
                    result[l] = result[r];
                    result[r] = temp;
                    l++;
                    r--;
                }
            }
        }
        String ans = new String(result);
        return ans;
    }

    public static void main(String[] args) {
        String s = "Let's take LeetCode contest";
        TestDemo test = new TestDemo();
        String str = test.reverseWords(s);
        System.out.println(str);
    }
}

